package com.gzw.dms.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.gzw.dms.R;
import com.gzw.dms.bean.EquipmentInfo;
import com.gzw.dms.bean.ShippingOrderInfo;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/12/1.
 */
public class EquipmentAdapter extends BaseAdapter {
    private Context mContext;
    private List<EquipmentInfo> lsInfo;
    private String user_name;
    private static HashMap<Integer, Boolean> isSelected;

    public EquipmentAdapter(Context _mContext,
                          List<EquipmentInfo> _lsInfo ,String _user_name) {
        this.mContext = _mContext;
        this.lsInfo = _lsInfo;
        this.user_name = _user_name;

        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }



    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < lsInfo.size(); i++) {
            if (lsInfo.get(i).getUsername().equals(user_name)){
                getIsSelected().put(i, true);

            }else {
                getIsSelected().put(i, false);

            }
        }


    }

    @Override
    public View getView(final int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        // 加载布局文件
        final  Holder mHolder;

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_equipment, null);
            mHolder = new Holder();
            mHolder.tv_hybh = (TextView) convertView.findViewById(R.id.tv_hybh);
            mHolder.tv_ztj = (TextView) convertView.findViewById(R.id.tv_ztj);
            mHolder.tv_chddh = (TextView) convertView.findViewById(R.id.tv_chddh);
            mHolder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);

            convertView.setTag(mHolder);
        } else {
            mHolder = (Holder) convertView.getTag();
        }

        EquipmentInfo shippingOrderInfo = lsInfo.get(arg0);
        mHolder.tv_hybh.setText("车牌号:"+"  "+shippingOrderInfo.getCar_number());
        mHolder.tv_ztj.setText("用户名:"+"  "+shippingOrderInfo.getUsername());
//        mHolder.tv_zdwz.setText("用户名:"+"  "+shippingOrderInfo.getUsername());
//        mHolder.tv_qysj.setText("起运时间:"+shippingOrderInfo.getSend_time());
        mHolder.tv_chddh.setText("司机姓名:"+"  "+shippingOrderInfo.getDrivername());
//        mHolder.tv_fhkh.setText("受理机构:"+"  "+shippingOrderInfo.getSend_mechanism());
//        mHolder.tv_shkh.setText("收货客户:" + "  " + shippingOrderInfo.getReceipt_name());
//        if(shippingOrderInfo.getAging_day().equals("0")) {
//            mHolder.tv_sx.setText("标准时效:"+ shippingOrderInfo.getAging_time() + "(当日)");
//        }else if (shippingOrderInfo.getAging_day().equals("1")) {
//            mHolder.tv_sx.setText("标准时效:" + shippingOrderInfo.getAging_time() + "(次日)");
//        }else if (shippingOrderInfo.getAging_day().equals("2")) {
//            mHolder.tv_sx.setText("标准时效:" + shippingOrderInfo.getAging_time() + "(隔日)");
//        }
        mHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //先把链表中的数据取代掉
                isSelected.put(arg0, isChecked);
                if (buttonView.isChecked())
                {
                    for(int i=0;i<lsInfo.size();i++)
                    {
                        //把其他的checkbox设置为false
                        if(i!=arg0){
                            isSelected.put(i, false);
                        }
                    }
                }
                //通知适配器更改
                EquipmentAdapter.this.notifyDataSetChanged();

            }
        });
        mHolder.cb.setChecked(getIsSelected().get(arg0));
        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        EquipmentAdapter.isSelected = isSelected;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return lsInfo.get(arg0);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return lsInfo.size();
    }

    public static class Holder {
        public TextView tv_hybh;
        public TextView tv_ztj;
        public TextView tv_zdwz;
        public TextView tv_qysj;
        public TextView tv_chddh;
        public TextView tv_fhkh;
        public TextView tv_shkh;
        public TextView tv_sx;
        public CheckBox cb;
    }
}
